package project.active;

import java.util.ArrayList;
import java.util.List;

import model.pojo.Employee;
import project.dao.CommentDAO;
import project.dao.EmployeeDAO;
import project.dao.ProjectDAO;
import project.dao.TestProjectDAO;
import project.model.PrComment;
import project.model.PrProject;
import project.model.PrTestProject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
//import android.widget.AdapterView;
//import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import approve.action.R;

public class Lvcommenterr extends Activity {
	Bundle language;
	String lang="EN",database="admin";
	PrProject pr =new PrProject();
	ProjectDAO prodao=new ProjectDAO();
	EmployeeDAO emdao=new EmployeeDAO();
	TestProjectDAO errdao= new TestProjectDAO();
	Employee emp=new Employee();
	CommentDAO comdao=new CommentDAO();
	TextView logout,username;
	Button btgoErrlist,btgoErr;
	TextView proinfo,errinfo;
	ListView lstcom;
	
	PrTestProject oneer=null;
	ArrayList<PrComment> lstcomlist = null;
	PrComment pcm=null;
	
	L_list_lstcom adcom;
	
	@SuppressWarnings("rawtypes")
	List map = null;
	int count = 0;
	LinearLayout listPages;
	TextView txt_first, txt_last,txt_prev,txt_next;
	ImageView img_next, img_prev;
	int pageSize = 6;
	int pageCount = 0;
	int maxPages = 2;
	int currentPage = 1;
	List<Integer> listCurrentPages = new ArrayList<Integer>();
	
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	@SuppressWarnings({ "static-access", "unchecked" })
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lvcommenterr);
		language = getIntent().getExtras().getBundle(getLang());
		
		btgoErrlist=(Button)findViewById(R.id.l_bt_ComtogoErrList);
		btgoErr=(Button)findViewById(R.id.l_bt_ComtoErr);
		proinfo=(TextView)findViewById(R.id.l_tv_proinfoCom);
		errinfo=(TextView)findViewById(R.id.l_tv_errinfocom);
		lstcom=(ListView)findViewById(R.id.l_lv_commenterr);
		
		btgoErrlist.setText(language.getString("l_errinfo"));
		btgoErr.setText(language.getString("error"));
		
		Intent ii=this.getIntent();
		pr=(PrProject)ii.getSerializableExtra("project");		
		emp=(Employee)ii.getSerializableExtra("em");
		oneer=(PrTestProject)ii.getSerializableExtra("oneerr");
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
		
		proinfo.setText(pr.getIdProject()+" - "+pr.getNameProject());
		
		String tnv=language.getString("error")+": "+oneer.getIdTest();
		Spannable WordtoSpan = new SpannableString(tnv);        
		WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(oneer.getColorText())), 0, tnv.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		errinfo.setText(WordtoSpan);
		
		txt_prev=(TextView) findViewById(R.id.txt_prev);txt_prev.setText("<<");
		txt_next=(TextView) findViewById(R.id.txt_next);txt_next.setText(">>");	
		
		if(oneer!=null){			
			map=comdao.getListComPG(pageSize, currentPage,oneer.getIdTest(),getDatabase());
			
			if (map != null && map.size() > 1) {
				lstcomlist = (ArrayList<PrComment>) map.get(0);
				count = (Integer) map.get(1);
			}
			
//			lstcomlist=comdao.getListCom(oneer.getIdTest(),getDatabase());
			if(lstcomlist!=null && lstcomlist.size()>0){
				//lstcom.setAdapter(new ArrayAdapter<PrComment>(this,android.R.layout.simple_expandable_list_item_1,lstcomlist));
				adcom = new L_list_lstcom(this, lstcomlist, language.getString("comment"));
				lstcom.setAdapter(adcom);
			}
			
			pageCount = (int) Math.ceil((double) count / pageSize);
			
			if (pageCount > 0) {
				txt_first = (TextView) findViewById(R.id.txt_first);
				if (currentPage != 1) {
					txt_first.setTextColor(Color.BLACK);
					txt_first.setOnClickListener(new View.OnClickListener() {
						// @Override
						public void onClick(View v) {
							currentPage = 1;
							onCreate(savedInstanceState);

						}
					});
				}
				txt_last = (TextView) findViewById(R.id.txt_last);
				if (currentPage != pageCount) {
					txt_last.setTextColor(Color.BLACK);
					txt_last.setOnClickListener(new View.OnClickListener() {
						// @Override
						public void onClick(View v) {
							currentPage = pageCount;
							onCreate(savedInstanceState);

						}
					});
				}
				
				
				listPages = (LinearLayout) findViewById(R.id.listpages);
				listCurrentPages.clear();
				final int secIndex = (int) Math.ceil((double) currentPage
						/ maxPages);
				//image next prev
				if (currentPage > maxPages) {				
					txt_prev.setTextColor(Color.BLACK);
					txt_prev.setOnClickListener(new View.OnClickListener() {
						// @Override
						public void onClick(View v) {
							int secIndexTemp = secIndex - 1;
							int minIndexTemp = (secIndexTemp * maxPages)
									- (maxPages - 1);
							currentPage = minIndexTemp;
							onCreate(savedInstanceState);
						}
					});

				}

				if (currentPage < pageCount) {
					txt_next.setTextColor(Color.BLACK);
					
					txt_next.setOnClickListener(new View.OnClickListener() {
						// @Override
						public void onClick(View v) {
							int secIndexTemp = secIndex + 1;
							int minIndexTemp = (secIndexTemp * maxPages)
									- (maxPages - 1);
							currentPage = minIndexTemp;
							onCreate(savedInstanceState);
						}
					});

				}
				//list pages
				int maxIndex = secIndex * maxPages;
				int minIndex = maxIndex - (maxPages - 1);

				if (maxIndex <= pageCount) {
					for (int i = minIndex; i <= maxIndex; i++) {
						final int index = i;
						TextView tv = new TextView(this);
						tv.setText(i + "");
						if (currentPage != i) {
							tv.setTextColor(Color.BLACK);
							tv.setOnClickListener(new View.OnClickListener() {
								// @Override
								public void onClick(View v) {
									currentPage = index;
									onCreate(savedInstanceState);
								}
							});
						}
						tv.setPadding(15, 0, 15, 0);

						listPages.addView(tv);
						listCurrentPages.add(i);
					}
				} else {
					for (int i = minIndex; i <= pageCount; i++) {
						final int index = i;
						TextView tv = new TextView(this);
						tv.setText(i + "");
						if (currentPage != i) {
							tv.setTextColor(Color.BLACK);
							tv.setOnClickListener(new View.OnClickListener() {
								// @Override
								public void onClick(View v) {
									currentPage = index;
									onCreate(savedInstanceState);
								}
							});
						}
						tv.setPadding(15, 0, 15, 0);

						listPages.addView(tv);
						listCurrentPages.add(i);
					}
				}
				
			}
		}
		
		btgoErrlist.setOnClickListener(goErrlist);
		btgoErr.setOnClickListener(goerr);
		//lstcom.setOnItemClickListener(comdetail);
	}
	
//	private OnItemClickListener comdetail = new OnItemClickListener() {
//
//		
//		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
//				long arg3) {
//			// TODO Auto-generated method stub
//			pcm= (PrComment) arg0.getItemAtPosition(arg2);
//			//System.out.println("com: "+pcm.getIdComment());
//			Intent i=new Intent(Lvcommenterr.this, Lvcomdetail.class);
//			i.putExtras(getIntent().getExtras());
//			i.putExtra("em",emp);
//			i.putExtra("lang", getLang());
//			i.putExtra("database", getDatabase());
//			i.putExtra("project", pr);
//			i.putExtra("oneerr", oneer);
//			i.putExtra("onecom", pcm);
//			
//			startActivity(i);
//		}
//	};
	
	private OnClickListener goErrlist = new OnClickListener() {
		
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Lvcommenterr.this, Lverrorlist.class);
			i.putExtras(getIntent().getExtras());
			i.putExtra("em",emp);
			i.putExtra("lang", getLang());
			i.putExtra("database", getDatabase());
			i.putExtra("project", pr);
			
			startActivity(i);
		}
	};
	
	private OnClickListener goerr = new OnClickListener() {
		
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Lvcommenterr.this, Lerrdetail.class);
			i.putExtras(getIntent().getExtras());
			i.putExtra("em",emp);
			i.putExtra("oneerr", oneer);
			i.putExtra("lang", getLang());
			i.putExtra("database", getDatabase());
			i.putExtra("project", pr);
			
			startActivity(i);
		}
	};
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Lvcommenterr.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
}
